package com.renli.unitTest;

import com.renli.mapper.EmployeeMapper;
import com.renli.pojo.Employee;
import com.renli.utils.JDBCUtils;
import org.junit.jupiter.api.*;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class EmployeeMapperTest {

    @BeforeEach
    public void setUp() {
        // 在每个测试方法之前执行的设置代码，例如初始化数据库连接等
        System.out.println(JDBCUtils.getConnection());
    }

    @Test
    @Order(1)
    public void testAddEmployee() {
        Employee employee = new Employee();
        employee.setFullName("John Doe");
        employee.setEmail("john.doe@example.com");
        employee.setPhone("123-456-7890");
        employee.setDepartment("HR");
        employee.setPosition("Manager");
//        employee.setJoinDate(new Date());
        employee.setSalary(50000.00);
        employee.setAvatar("avatar_url_here");

        try {
            boolean result = EmployeeMapper.add(employee);
            assertTrue(result);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(2)
    public void testGetEmployeeById() {
        int employeeId = 13; // 假设存在员工ID为1的员工
        try {
            Employee employee = EmployeeMapper.getEmployeeById(employeeId);
            assertNotNull(employee);
            System.err.println(employee);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(3)
    public void testUpdateEmployee() {
        Employee employee = new Employee();
        employee.setEmployeeId(13); // 假设要更新员工ID为1的员工信息
        employee.setFullName("Updated Name");
        employee.setEmail("updated.email@example.com");
        employee.setPhone("987-654-3210");
        employee.setDepartment("Updated Department");
        employee.setPosition("Updated Position");
//        employee.setJoinDate(new Timestamp());
        employee.setSalary(60000.00);
        employee.setAvatar("updated_avatar_url_here");

        try {
            boolean result = EmployeeMapper.update(employee);
            assertTrue(result);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(4)
    public void testGetTotalEmployeeCount() {
        try {
            int totalEmployeeCount = EmployeeMapper.getTotalEmployeeCount();
            assertTrue(totalEmployeeCount > 0);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(5)
    public void testGetEmployeesByPage() {
        String fullName = "U"; // 假设要搜索的员工名字包含"John"
        int pageNum = 1; // 假设要查询第一页
        int pageSize = 10; // 假设每页显示10条记录
        try {
            List<Employee> employees = EmployeeMapper.getEmployeesByPage(fullName, pageNum, pageSize);
            System.err.println(employees);
            assertNotNull(employees);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }

    @Test
    @Order(6)
    public void testDeleteEmployee() {
        int employeeId = 1; // 假设要删除员工ID为12的员工
        try {
            boolean result = EmployeeMapper.delete(employeeId);
            assertTrue(result);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
        System.out.println("testDeleteEmployee()");
    }


    @Test
    @Order(7)
    public void testDeleteBatch() {
        List<Integer> employeeIds = new ArrayList<>();
        employeeIds.add(1); // 假设要删除员工ID为1的员工
        employeeIds.add(2); // 假设要删除员工ID为2的员工
        try {
            boolean result = EmployeeMapper.deleteBatch(employeeIds);
            assertTrue(result);
        } catch (SQLException e) {
            fail("Exception thrown: " + e.getMessage());
        }
    }
}
